import {defineStore} from 'pinia';
import {
    AssetList,
    getAssetDetail,
    addAsset,
    deleteAsset,
    updateAsset,
    importAsset,
    exportAsset
} from "@/api/index.js";
import {ElMessage} from 'element-plus';

export const adminAssetManage = defineStore('assetManager', {
    state: () => ({
        assetList: [],
        total: 0,
        queryParams: {
            page: 1,
            pageSize: 10,
            assetName: '',
            categoryName: '',
            statusName: '',
        },
        loading: false,
        assetDetail: null, // 用于存储资产详情
        detailLoading: false, // 加载详情的 loading 状态
    }),
    getters: {},
    actions: {
        // 获取资产列表数据
        async getAssets() {
            this.loading = true;
            try {
                const response = await AssetList({
                    page: this.queryParams.page,
                    pageSize: this.queryParams.pageSize,
                    assetName: this.queryParams.assetName,
                    categoryName: this.queryParams.categoryName,
                    statusName: this.queryParams.statusName,
                });
                if (response.code === 200) {
                    this.assetList = response.data.records;
                    this.total = response.data.total;
                } else {
                    ElMessage.error({message: "获取资产列表失败", duration: 3000});
                    console.error(response.message || '获取资产列表失败');
                }
            } catch (error) {
                ElMessage.error({message: "获取资产列表失败", duration: 3000});
                console.error(error.message || '获取资产列表失败');
            } finally {
                this.loading = false;
            }
        },
        // 获取资产详情
        async fetchAssetDetail(id) {
            this.detailLoading = true;
            this.assetDetail = null; // 清空之前的详情数据
            try {
                const response = await getAssetDetail(id);
                if (response.code === 200) {
                    this.assetDetail = response.data;
                } else {
                    ElMessage.error({message: "获取资产详情失败", duration: 3000});
                    console.error(response.message || '获取资产详情失败');
                }
            } catch (error) {
                ElMessage.error({message: "获取资产详情失败", duration: 3000});
                console.error(error.message || '获取资产详情失败');
            } finally {
                this.detailLoading = false;
            }
        },
        // 新增资产
        async createAsset(params) {
            try {
                const response = await addAsset(params);
                if (response.code === 200) {
                    ElMessage.success({message: "新增资产成功", duration: 3000});
                } else {
                    ElMessage.error({message: response.message || "新增资产失败", duration: 3000});
                }
            } catch (error) {
                ElMessage.error({message: error.message || "新增资产失败", duration: 3000});
            }
        },
        // 更新资产
        async modifyAsset(id, params) {
            try {
                const response = await updateAsset(id, params);
                if (response.code === 200) {
                    ElMessage.success({message: "更新资产成功", duration: 3000});
                } else {
                    ElMessage.error({message: response.message || "更新资产失败", duration: 3000});
                }
            } catch (error) {
                ElMessage.error({message: error.message || "更新资产失败", duration: 3000});
            }
        },
        // 删除资产
        async removeAsset(id) {
            try {
                const response = await deleteAsset([id]);
                if (response.code === 200) {
                    ElMessage.success({message: "删除资产成功", duration: 3000});
                } else {
                    ElMessage.error({message: response.message || "删除资产失败", duration: 3000});
                }
            } catch (error) {
                ElMessage.error({message: error.message || "删除资产失败", duration: 3000});
                console.log(error.message)
            }
        },
        // 导入资产
        async uploadAsset(data) {
            try {
                const response = await importAsset(data);
                if (response.code === 200) {
                    ElMessage.success({message: "导入资产成功", duration: 3000});
                } else {
                    ElMessage.error({message: response.message || "导入资产失败", duration: 3000});
                }
            } catch (error) {
                ElMessage.error({message: error.message || "导入资产失败", duration: 3000});
            }
        },
        // 导出资产
        async downloadAsset(params) {
            try {
                const response = await exportAsset(params);
                if (response.code === 200) { // 假设 200 是成功状态码
                    ElMessage.success({message: "导出资产成功", duration: 3000});
                } else {
                    ElMessage.error({message: response.message || "导出资产失败", duration: 3000});
                    throw new Error(response.message || "导出资产失败");
                }
            } catch (error) {
                ElMessage.error({message: error.message || "导出资产失败", duration: 3000});
                throw error;
            }
        },
        // 设置当前页码
        setPage(page) {
            this.queryParams.page = page;
        },
        // 设置每页显示记录数
        setPageSize(pageSize) {
            this.queryParams.pageSize = pageSize;
        },
        // 重置查询参数
        resetQuery() {
            this.queryParams.assetName = '';
            this.queryParams.categoryName = '';
            this.queryParams.statusName = '';
            this.queryParams.page = 1;
        },
    },
});